package objects
{
	import flash.events.Event;
	import flash.geom.Point;
	import objects.cicleItem.cicleItemBaseBuild;
	import starling.display.Image;
	import starling.display.MovieClip;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.events.Touch;
	import starling.events.TouchEvent;
	import starling.events.TouchPhase;
	import starling.filters.BlurFilter;
	import com.greensock.*;
	import com.greensock.easing.*;
	
	/**
	 * ...
	 * @author ...Vượng
	 */
	public class BaseBuild extends MyObject
	{
		public var m_cicleItem:cicleItemBaseBuild;
		public var m_testChoose:Boolean;
		public var m_sprite:Sprite;
		public var m_testBuy:Boolean;
		public var m_cointTotal:Number;
		
		public function BaseBuild(_sprite:Sprite, _pos:Point,_coint:int)
		{
			m_sprite = _sprite;
			m_pos = _pos;
			m_cicleItem = new cicleItemBaseBuild(this, m_pos);
			this.addEventListener(starling.events.Event.ADDED_TO_STAGE, init);
			m_sprite.addEventListener(TouchEvent.TOUCH, onTouch);
			m_sprite.addChild(this);
			m_testBuy = false;
			this.m_cointTotal = _coint;
		}
		
		private function onTouch(e:TouchEvent):void
		{
			if (database.instance.m_testTouch == true)
			{
				var touch:Touch = e.getTouch(stage);
				if (touch)
				{
					
					var _pointTouch:Point = new Point(touch.globalX, touch.globalY);
					if (touch.phase == TouchPhase.BEGAN)
					{
						if (m_spriteImage.bounds.containsPoint(_pointTouch) == true)
						{
							m_cicleItem.showCoint(m_cointTotal);
							m_cicleItem.clockAndUnclock();
							m_cicleItem.showCoint(m_cointTotal);
							if (m_testChoose == false)
							{
								m_testChoose = true;
								m_sprite.setChildIndex(this, m_sprite.numChildren);
								this.setChildIndex(m_cicleItem, this.numChildren);
							}
							else
							{
								m_testChoose = false;
							}
						}
						else
						{
							m_cicleItem.returnResult(_pointTouch);
							m_testBuy = true;
							m_testChoose = false;
							if (m_cicleItem.m_result == -1)
							{
								m_testChoose = true;
							}
						}
					}
					else if (touch.phase == TouchPhase.HOVER)
					{
						if (m_spriteImage.bounds.containsPoint(_pointTouch) == true)
						{
							m_spriteImage.filter = BlurFilter.createDropShadow(0, 0, 0xff0000, 1, 1, 0.5);
						}
						else
						{
							m_spriteImage.filter = null;
						}
						
						m_cicleItem.blurFilter(_pointTouch);
						m_testBuy = true;
					}
				}
			}
		}
		
		override public function update(dt:Number):void
		{
			if (m_testChoose == true)
			{
				m_cicleItem.alphaUp();
			}
			else
			{
				m_cicleItem.alphaDown();
			}
		}
		public function buyHouse(dt:Number,coint:int):int
		{
			if (m_testBuy == true)
			{
				if (m_cicleItem.m_result == 1&&m_cicleItem.m_baseItem[0].m_coint<=coint)
				  {
					  return coint -= m_cicleItem.m_baseItem[0].m_coint;
				  }
				  if (m_cicleItem.m_result == 2&&m_cicleItem.m_baseItem[1].m_coint<=coint)
				  {
					  return coint -= m_cicleItem.m_baseItem[1].m_coint;
				  }
				  if (m_cicleItem.m_result == 3&&m_cicleItem.m_baseItem[2].m_coint<=coint)
				  {
					  return coint -= m_cicleItem.m_baseItem[2].m_coint;
				  }
				  if (m_cicleItem.m_result == 4&&m_cicleItem.m_baseItem[3].m_coint<=coint)
				  {
					  return coint -= m_cicleItem.m_baseItem[3].m_coint;
				  }
			}
			
			m_testBuy = false;
			return coint;
		}
		private function init(e:starling.events.Event):void
		{
			removeEventListener(starling.events.Event.ADDED_TO_STAGE, init);
			//anh nen
			m_spriteImage = new Image(Assets.getTexture("BASE_BUILD_NONE"));
			m_spriteImage.filter = null;
			this.addChild(m_spriteImage);
			m_spriteImage.visible = true;
			
			this.setAnchorPoint();
			this.setPos(m_pos);
		}
		
		public function setAnchorPoint():void
		{
			m_spriteImage.pivotX = m_spriteImage.width / 2;
			m_spriteImage.pivotY = m_spriteImage.height / 2;
		}
		
		public function setPos(_pos:Point):void
		{
			m_spriteImage.x = _pos.x;
			m_spriteImage.y = _pos.y;
		}
		
		public function setScale(_scale:Number):void
		{
			m_spriteImage.scaleX = _scale;
			m_spriteImage.scaleY = _scale;
		}
		public function releaseFull(_listContain:Array,_index:int):void
		{	
			this.removeChildren();
			m_cicleItem.release();
			m_sprite.removeChild(this);
			_listContain.splice(_index, 1);
		}
	}

}